import java.util.ArrayList;
public class Pagination {
	protected static int PageShow;
	protected static int PStart;
	protected static int PEnd;
	protected static int TPage;
	protected static int NTP;  // Next To Page
	protected Artical ar=null;
	public static ArrayList<Integer> list_main=new ArrayList<>();
	public static ArrayList<Integer> listNameFile=new ArrayList<>();
	//==== constructor pagination =========
	public Pagination(){
		listNameFile=FileArtical.listFileName();
		PageShow=5;
		PStart=0;
		PEnd=PageShow;
		TPage=listNameFile.size();
	}
	//==== get total page =========
	public int GetTotalPage(){
		return listNameFile.size();
	}
	//==== set first page=========
	public void First(){
		PStart=0;
		PEnd=PageShow;
	}
	//==== set back page=========
	public void Back(){
		if(PStart!=0){
			PStart=PStart-PageShow;
			PEnd=PEnd-PageShow;
		 }
	}
	//==== set next page=========
	public void Next(){
		if(PageShow<=TPage){
			 PStart+=PageShow;
			 PEnd+=PageShow;
		}
	}
	//==== set last page=========
	public void Last(){
		int p=GetTotalPage()/PageShow;
		if(p>0){
			int p1=GetTotalPage()%PageShow;
			if(p1==0)p-=1;
		}
		if(p==0)p=0;
		PStart=PageShow*(p);
		PEnd=GetTotalPage();
		
	}
	//==== set next to  page=========
	public void NextToPage(int ntp){
		NTP=ntp;
		PStart=PageShow*(NTP-1);
		PEnd=PStart+PageShow;
	}
	//==== set rows page=========
	public void setRow(int r){
		PageShow=r;
		PStart=0;
		PEnd=PageShow;
	}
	//==== show pagination=========
	public void ShowPagination(){
		Display.ClearScreen(3);
		FileArtical fa=new FileArtical();
		int Count=GetTotalPage();
		int pagin=Count/PageShow;
		int p1=Count%PageShow;
		if(p1>0)pagin+=1;
		int p=(PStart/PageShow)+1;
		System.out.println("-------------------------------------------------------------------------------------------------");
		String leftAlignFormat = "| %-14s | %-25s | %-26s| %-20s |%n";
		System.out.format("+================+===========================+===========================+======================+%n");
		System.out.printf("|       No       |           Title           |          Author           |          Date        |%n");
		System.out.format("+================+===========================+===========================+======================+%n");
		for(int i=PStart;i<PEnd;i++){	
			if(i<Count){
				String st=listNameFile.get(i)+"";
				ar=new Artical();
				ar=fa.read(st);
				System.out.format(leftAlignFormat,ar.getId(),Display.showDetail(ar.getTitle(), 19),Display.showDetail(ar.getAuthor(), 18),ar.getDate());
				System.out.format("+----------------+---------------------------+---------------------------+----------------------+%n");
			}
			}
			System.out.println("Total: "+Count+"                                                                          Page:"+p+"/"+pagin);
			System.out.format("-------------------------------------------------------------------------------------------------%n");
	}

}
